import { Controller } from "../../mvc/core/Controller";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SCampTransportInfo } from "../../../Message/Public/SCampTransportInfo";
import { TaskInfo } from "../model/TaskInfo";
import { ETaskGroup } from "../../../Message/Game/ETaskGroup";
import { EffectManager } from "../manager/EffectManager";
import { UIIconEvent } from "../view/uiIconBtn/UIIconEvent";
import { TransportActHintIcon } from "../view/activity/TransportActHintIcon";
import { UiHintIconView } from "../view/uiIconBtn/UiHintIconView";
import { Language } from "../../../extend/language/Language";
import { HTMLUtil } from "../../../com/gengine/utils/HTMLUtil";
import { ActivityTransportModule } from "../../../modules/ActivityTransportModule";
import { LayerManager } from "../manager/LayerManager";
import { MsgManager } from "../manager/MsgManager";
import { GameController } from "../mvc/GameController";
import { ChatType } from "../view/chat/chatViewData/ChatType";
import { ChatArea } from "../view/chat/ChatArea";
import { SecTimer } from "../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../com/gengine/core/frame/TimerType";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { ServerCommand } from "../mvc/ServerCommand";
import { IView } from "../../mvc/interfaces/IView";
import { IActivityTransportModule } from "../../../modules/interfaces/IActivityTransportModule";
//class ActivityController
    
    export  class ActivityController extends Controller
    {
       

        /* internal  */onCampTransportInfoRes(arg1: MessageBlock): void
        {
            cache.activity.transport = arg1.messageBase as SCampTransportInfo;
            var loc1=cache.activity.transport.flag;
            switch (loc1) 
            {
                case 0:
                {
                    break;
                }
                case 1:
                {
                    this.onActivityTransportStart();
                    break;
                }
                case 2:
                {
                    this.onActivityTransportEnd();
                    break;
                }
                case 3:
                {
                    break;
                }
            }
            return;
        }

        /* internal  */onTaskGetRes(arg1: Object): void
        {
            if (arg1 == null) 
            {
                return;
            }
            var loc1=arg1.task as TaskInfo;
            if (loc1 != null) 
            {
                if (loc1.sTask.group == ETaskGroup._ETaskGroupTransport) 
                {
                    if (this._transportIcon && this._transportIcon.parent) 
                    {
                        this._transportIcon.parent.removeChild(this._transportIcon);
                        EffectManager.glowFilterUnReg(this._transportIcon);
                        this._transportIcon.dispatchEvent(new UIIconEvent(UIIconEvent.RemoveIcon));
                    }
                    if (this._transport && !this._transport.isHide) 
                    {
                        this._transport.hide();
                    }
                }
            }
            return;
        }

        /* internal  */onTaskEndRes(arg1: Object): void
        {
            if (!cache.activity.transport || !(cache.activity.transport.flag == 1)) 
            {
                return;
            }
            if (arg1 == null) 
            {
                return;
            }
            var loc1=arg1.task as TaskInfo;
            if (loc1 != null) 
            {
                if (loc1.sTask.group == ETaskGroup._ETaskGroupTransport) 
                {
                    if (cache.daily.transportInfo.num != cache.daily.transportInfo.totalNum) 
                    {
                        if (!this._transportIcon) 
                        {
                            this._transportIcon = new TransportActHintIcon();
                            this._transportIcon.addEventListener(UIIconEvent.RemoveIcon, this.onRemoveIconHandler);
                        }
                        if (!this._transportIcon.parent) 
                        {
                            UiHintIconView.instance.addHintIcon(this._transportIcon);
                        }
                    }
                }
            }
            return;
        }

        public onActivityTransportStart(): void
        {
            if (cache.task.getCanGetTaskByGroup(ETaskGroup._ETaskGroupTransport).length != 0) 
            {
                if (!this._transportIcon) 
                {
                    this._transportIcon = new TransportActHintIcon();
                    this._transportIcon.addEventListener(UIIconEvent.RemoveIcon, this.onRemoveIconHandler);
                }
                UiHintIconView.instance.addHintIcon(this._transportIcon);
            }
            this._transportChat = "<MsgObj>COLOR,4," + cache.activity.transportStartTime + "~" + cache.activity.transportEndTime + "</MsgObj>" + Language.getString(20004) + "<MsgObj>COLOR,3," + Language.getString(20005) + "</MsgObj>" + "，" + Language.getString(20006) + "<MsgObj>TRAN,</MsgObj>" + Language.getString(20007) + "<MsgObj>COLOR,6," + Language.getString(20008) + "</MsgObj>" + Language.getString(20009) + "~！";
            this._transportInfo = HTMLUtil.addColor(cache.activity.transportStartTime + "~" + cache.activity.transportEndTime, "#ff00ff");
            this._transportInfo = this._transportInfo + Language.getString(20004);
            this._transportInfo = this._transportInfo + HTMLUtil.addColor(Language.getString(20005), "#00fffff");
            this._transportInfo = this._transportInfo + ("，" + Language.getString(20006));
            this._transportInfo = this._transportInfo + HTMLUtil.addColor(Language.getString(20281), "#ff00ff");
            this._transportInfo = this._transportInfo + (Language.getString(20007) + HTMLUtil.addColor(Language.getString(20008), "#ff0000") + Language.getString(20009) + "~！");
            this._transportInfo = HTMLUtil.addColor(this._transportInfo, "#ffffff");
            this.onTimerHandler(null);
            this.startTimer();
            return;
        }

        /* internal  */onRemoveIconHandler(arg1: UIIconEvent): void
        {
            if (!this._transport) 
            {
                this._transport = new ActivityTransportModule();
                this._transport.layer = LayerManager.windowLayer;
            }
            this._transport.transportStart();
            this._transport.show();
            return;
        }

        /* internal  */onActivityTransportEnd(): void
        {
            var loc1=HTMLUtil.addColor(Language.getString(20282), "#ffffff");
            MsgManager.addBroadCast(loc1);
            GameController.chat.addTipMsg(Language.getString(20282), ChatType.Tips, ChatArea.World, 2);
            if (this._transportIcon && this._transportIcon.parent) 
            {
                this._transportIcon.parent.removeChild(this._transportIcon);
                EffectManager.glowFilterUnReg(this._transportIcon);
                this._transportIcon.dispatchEvent(new UIIconEvent(UIIconEvent.RemoveIcon));
            }
            if (this._transport && !this._transport.isHide) 
            {
                this._transport.hide();
            }
            this.endTimer();
            return;
        }

        /* internal  */startTimer(): void
        {
            if (!this._activityTimer) 
            {
                this._activityTimer = new SecTimer(600);
                this._activityTimer.addListener(TimerType.ENTERFRAME, this.onTimerHandler);
            }
            if (!this._activityTimer.running) 
            {
                this._activityTimer.start();
            }
            return;
        }

        /* internal  */endTimer(): void
        {
            if (this._activityTimer && this._activityTimer.running) 
            {
                this._activityTimer.stop();
                this._activityTimer.isDelete = false;
            }
            return;
        }

        /* internal  */onTimerHandler(arg1: SecTimer): void
        {
            GameController.chat.addTipMsg(this._transportChat, ChatType.Tips, ChatArea.World, 2);
            MsgManager.addBroadCast(this._transportInfo);
            return;
        }

        /* internal  */onActiveAddTransportHandler(arg1: SActiveToClient): void
        {
            if (this._transportIcon) 
            {
                this._transportIcon.updateActiveToClientInfo(arg1);
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCampTransportInfo, this.onCampTransportInfoRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.onTaskGetRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskEndRes, this.onTaskEndRes);
            NetDispatcher.addCmdListener(ServerCommand.ActiveAddTransport, this.onActiveAddTransportHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            return super.initView();
        }

        private /* var */_transport: IActivityTransportModule;

        private /* var */_transportIcon: TransportActHintIcon;

        private /* var */_activityTimer: SecTimer;

        private /* var */_transportInfo: string;

        private /* var */_transportChat: string;
    }
